<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/26
 * Time: 22:04
 */
namespace Admin\Controller;
// use Think\Controller;
class DeptController extends CommonController{
   public function add(){
       $model=D("dept");
       //判断请求类型
       if(IS_POST){
            //处理表单提交
//           $post=I('post.');
//            $result=$model->add($post);
           $data=$model->create();
            if(!$data){
                //exit:退出
                $this->error($model->getError());exit;
            }
           $result=$model->add();
            if($result){
                $this->success('添加成功',U('showList'),3);
            }else{
                $this->error('添加失败');
            }
       }else{
           //查询出顶级部门
           $result=$model->select();
           $this->assign('result',$result);
           $this->display();
       }

   }
    public function showList(){
       //模型实例化
        $model=M('dept');
        $data=$model->order('id desc')->select();
        foreach($data as $key=>$val){
            if($val['pid']>0) {
                $info = $model->find($val['id']);
                $data[$key]['deptname'] = $info['name'];
            }
        }

        $this->assign('data',$data);
       $this->display();
    }

    public function edit(){
        $model=M("dept");
        $arr=array('我','你');dump($arr);die;
        if(IS_POST){
            $post=I('post.');
            $result=$model->save($post);
            if($result !== false ){
                $this->success('修改成功',U('showList'),3);
            }else{
                $this->error('修改失败');
            }
        }else{
            $id=I('get.id');
            $data=$model->find($id);
            $info=$model->where('id!='.$id)->select();
            $this->assign('data',$data);
            $this->assign('info',$info);
            $this->assign('arr',$arr);
            $this->display();
        }
    }

    public function del(){
      $id=I('get.id');
      $model=M('Dept');
      $result=$model -> delete($id);
      if($result){
        $this->success('删除成功');
      }else{
        $this->error('删除失败！');
      }
    }
}